package crp.core.security.domain.services.caller;

import com.qwlabs.cdi.Dispatchable;
import com.qwlabs.security.Caller;
import com.qwlabs.security.CallerPermissions;
import com.qwlabs.security.CallerPermissionsLoader;
import crp.core.security.Callers;
import crp.core.security.domain.services.PermissionLoader;
import org.checkerframework.checker.nullness.qual.Nullable;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

@ApplicationScoped
public class UserCallerPermissionsLoader implements CallerPermissionsLoader, Dispatchable<Caller> {
    private final PermissionLoader permissionLoader;

    @Inject
    public UserCallerPermissionsLoader(PermissionLoader permissionLoader) {
        this.permissionLoader = permissionLoader;
    }

    @Override
    public CallerPermissions permissions(Caller caller) {
        return UserCallerPermissions.builder()
            .caller(caller)
            .permissionLoader(permissionLoader)
            .build();
    }

    @Override
    public boolean dispatchable(@Nullable Caller context) {
        return Callers.isUser(context);
    }
}
